/*
  * gere une seule connexion
  */
function Connexion() {
	this.oCnx = null;
	this.creation = true;
	this.occupee = true;
	this.active = false;
	this.principale = false;
	this.cmdEnCours = null;
	this.cmdAEnvoyer = new Array();
	this.cmdEnAttente = null;
	
	this.listenerCmd = null;
	this.listenerData = null;
	
	
	this.init = function(createur) {
		this.oCnx = createur;
		var host = this.oCnx.oServeur.host;
		
		// Traitement de l'host
		host = host.replace(/^ftp:\/*/, ''); // On enleve le tag ftp:// si il se trouve en debut de chaine
		if (host.charAt(host.length - 1) == '/') {
			host = host.substring(0, host.length - 1); // On enleve le / final si il y en a un
		}
		
		var socketTransport = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
		var transport = socketTransport.createTransport(null, 0, host, parseInt(this.oCnx.oServeur.port), null);

		var output = transport.openOutputStream(0, 0, 0); // Pour pouvoir ecrire et envoyer les commandes
		var input = transport.openInputStream(0, 0, 0); // Permet de lire le retour

		var inputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
		inputStream.init(input);

		this.listenerCmd = new this.DataListenerCmd(); // Ceci permet de lire imediatement les donnees quand elles arrivent
		this.listenerCmd.me = this;
		this.listenerCmd.input = inputStream;
		this.listenerCmd.output = output;
		this.listenerCmd.transport = transport;
		this.listenerCmd.host = host;
		
		//listenerCmd.AddCmd("PASV");
		//listenerCmd.AddCmd("LIST " + escape(sPath) + " " + sIdView);

		var reader = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
		reader.init(input, -1, -1, 0, 0, false);
		reader.asyncRead(this.listenerCmd, null);
	}
	
	this.close = function() {
		//quit et delete
	}
	
	this.DataListenerCmd = function() {
		this.me = null;
		this.input = null;		// A initialiser lors de l'instanciation
		this.output = null; 	// A initialiser lors de l'instanciation
		this.transport = null;	// A initialiser lors de l'instanciation
		this.host = '';		// A initialiser lors de l'instanciation
		this.donnee = '';	// Donnees recu du ftp
		
		
		this.init = function(input, output, transport) {
			//marche pas.
		}
		
		this.onDataAvailable = function(request, context, inputStream, offset, count) {
			this.donnee = this.input.read(count);
			oInterface.updateStatus(this.donnee);
			
			//il faut spliter la reponse car elle peut contenir plusieurs reponses.
			var data = this.donnee.split('\r\n');
			for (var i in data)
			{
				var code = data[i].substring(0, 4);
				if(code == '220 ') {
					this.occupee = false;
					this.active = true;
				} else if(code == '230 ') {
					this.occupee = false;
					this.active = true;
				} else if(code == '331 ') {
					var tmpCmd = 'PASS ' + this.me.oCnx.oServeur.pwd;
					this.output.write(tmpCmd + '\r\n', tmpCmd.length + 2);
					oInterface.updateStatus(tmpCmd);
				}
			}
		}
		
		this.onStartRequest = function(request, context) {
			this.creation = false;
			oInterface.updateStatus('START');
			var tmpCmd = 'USER ' + this.me.oCnx.oServeur.login;
			this.output.write(tmpCmd + '\r\n', tmpCmd.length + 2);
			oInterface.updateStatus(tmpCmd);
		}
		
		this.onStopRequest = function(request, context, status) {
			this.active = false;
			oInterface.updateStatus('STOP');
		}
	}
	
	this.DataListenerDonnee = function() {
		this.input = null;		// A initialiser lors de l'instanciation
		this.output = null;		// A initialiser lors de l'instanciation
		this.input_old = null;	// A initialiser lors de l'instanciation ==>> cet input permet d'initialiser l'inputbinary il est juste ici pour pouvoir le fermer lors de la fermetur de la connexion
		this.output_old = null;
		this.socket = null;		// A initialiser lors de l'instanciation
		this.donnee = ''; // donnees recu
		
		this.onDataAvailable = function(request, context, inputStream, offset, count) {
		}
		this.onStartRequest = function(request, context) {
		}
		this.onStopRequest = function(request, context, status) {
		}
	}
	
	this.UploadSink = function() {
		this.onTransportStatus = function(transport, status, progress, progessMax) {
		}
	}
}
